package com.dream.keigezhushou.Activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.NiesalepriceInfo;
import com.dream.keigezhushou.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;

import static com.dream.keigezhushou.Activity.Uiutils.UiUtils.getContext;

public class EverDaySalepirceActivity extends BaseActivity implements View.OnClickListener {

    private ArrayList<NiesalepriceInfo> list = new ArrayList<>();
    private PullToRefreshListView mMpull;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ever_day_salepirce);


        mMpull = (PullToRefreshListView) findViewById(R.id.pulllist);
        View rootView = UiUtils.inflateView(R.layout.everday_ktvbuyfragment);

        ListView mlist =   mMpull.getRefreshableView();
        mlist.addHeaderView(rootView);

        ImageView ivback = (ImageView) findViewById(R.id.back);
        ivback.setOnClickListener(this);
        ImageView ivtitle = (ImageView) rootView.findViewById(R.id.iv_everyday);
        ivtitle.setOnClickListener(this);

        initData();//假数据
        //getDataFromService();   获取网络数据并解析
        // 加载动画
        //设置上拉刷新重新请求网络数据  更新adapter?


        mlist.setAdapter(new MyAdapter(list));

        mlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                UiUtils.toast("跳到KTV详情页");
                Intent intent = new Intent(UiUtils.getContext(), EveryDetailActivity.class);//Ktv详情页面
//                intent.putExtra("position", position);
             //   intent.putExtra("url", url);
                startActivity(intent);
            }
        });


    }

    private void initData() {
        for (int i =0;i<18;i++){
            NiesalepriceInfo info = new NiesalepriceInfo();
            info.setDetails("阳光欢唱3小时");
            info.setDistance("1.1km");
            info.setIv(getResources().getDrawable(R.mipmap.ktv_icon));
            info.setKtvname("魔方KTV小寨店");
            info.setOriginal_price("98");
            info.setPrice("31");
            list.add(info);
        }



    }

    @Override
    public void onClick(View v) {
   switch(v.getId()){
       case R.id.back:
           finish();
       break;
       case R.id.iv_everyday:
           UiUtils.toast("进去天天特价主页");
       break;

   }
    }


    class MyAdapter extends BaseAdapter {
        private ArrayList<NiesalepriceInfo> list;
        public MyAdapter(ArrayList<NiesalepriceInfo>list) {
            this.list =list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public NiesalepriceInfo getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder =null;
            if(convertView==null){
                convertView= View.inflate(getContext(), R.layout.item_niesing,null);
                holder =new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.ktv_icon);
                holder.ktvname = (TextView) convertView.findViewById(R.id.ktvname);
                holder.distance = (TextView) convertView.findViewById(R.id.ktv_distance);
                holder.details = (TextView) convertView.findViewById(R.id.details);
                holder.price = (TextView) convertView.findViewById(R.id.current_price);
                holder.original_price = (TextView) convertView.findViewById(R.id.original_price);
                holder.tv = (TextView) convertView.findViewById(R.id.panic_buying);

                convertView.setTag(holder);


            }else{
                holder= (ViewHolder) convertView.getTag();
            }

            NiesalepriceInfo info = getItem(position);
            holder.original_price.setText(info.getOriginal_price());
            holder.price.setText(info.getPrice());
            holder.details.setText(info.getDetails());
            holder.distance.setText(info.getDistance());
            holder.ktvname.setText(info.getKtvname());
            holder.iv.setImageDrawable(info.getIv());

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UiUtils.toast("跳抢购页面");
                    Intent intent = new Intent(EverDaySalepirceActivity.this, SubmitFromActivity.class);
                    startActivity(intent);

                }
            });

            return convertView;
        }
    }
    static class ViewHolder{
        private ImageView iv;
        private TextView ktvname;
        private TextView details;
        private TextView distance;
        private TextView  price;
        private TextView original_price;
        private  TextView tv;

    }
}
